﻿using System;
using System.IO;
using Microsoft.Build.Framework;
using NUnit.Framework;
using NUnit.Framework.SyntaxHelpers;
using QuickTest.Core.Compilation;
using Rhino.Mocks;

namespace QuickTest.Tests.Core.Compilation
{
    [TestFixture]
    public class MSBuildCompilerTests
    {
        #region TestData

        private const string EmbeddedResourceCompilationProjectPath = "QuickTest.Tests, QuickTest.Tests.Core.Compilation.Artifacts.CompilationTestProject.csproj";
        private const string EmbeddedResourceCompilationClassPath = "QuickTest.Tests, QuickTest.Tests.Core.Compilation.Artifacts.TestClass.cs";

        public const string CompilationProjectFilePath = TestUtils.TestDirectory + "/CompilationTestProject.csproj";
        public const string CompilationClassFilePath = TestUtils.TestDirectory + "/TestClass.cs";
        private const string OutputDirectory = TestUtils.TestDirectory + "/bin/debug/";
        private const string OutputAssemblyName = "CompilationTestProject.dll";

        public static void CreateCompilationTestProject()
        {
            TestUtils.CreateTestFile(EmbeddedResourceCompilationProjectPath, CompilationProjectFilePath);
            TestUtils.CreateTestFile(EmbeddedResourceCompilationClassPath, CompilationClassFilePath);
        }

        #endregion

        [SetUp]
        public void SetUp()
        {
            CreateCompilationTestProject();            
        }

        [TearDown]
        public void TearDown()
        {
            TestUtils.DeleteTestFiles();            
        }

        [Test]
        public void ArgumentNullException_Thrown_If_Compile_With_Null_FileUri()
        {
            TestUtils.AssertThrows<ArgumentNullException>(() =>
                                                          new MSBuildCompiler().Compile(null), "ArgumentNullException should have been thrown.");
        }

        [Test]
        [Category("Integration")]
        public void Can_Compile()
        {
            new MSBuildCompiler().Compile(new FileUri(CompilationProjectFilePath));

            Assert.That(File.Exists(string.Format("{0}/{1}", OutputDirectory, OutputAssemblyName)), "Compilation should have created assembly");
        }

        [Test]
        public void should_initialize_logger()
        {
            var logger = MockRepository.GenerateStub<ILogger>();

            new MSBuildCompiler(logger).Compile(new FileUri(CompilationProjectFilePath));

            logger.AssertWasCalled(l => l.Initialize(Arg<IEventSource>.Is.NotNull));
        }
    }
}